<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;

/**
 * 基本资料
 *
 * @icon fa fa-circle-o
 */
class Material extends Backend
{

    /**
     * Material模型对象
     * @var \app\admin\model\Material
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Material;
        $this->view->assign("sexList", $this->model->getSexList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index()
    {
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $admin_data = Db::name("admin")->where(['id'=>$this->auth->id])->find();
            //echo $admin_data['device_ids'];
            if($admin_data['username'] == 'admin'){
                $list = $this->model
                   // ->with('user')
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);
            }else{
                $list = $this->model
                    ->with('user')
                    ->where($where)
                    ->where('user.agency_id',$admin_data['agency_id'])
                    ->order($sort, $order)
                    ->paginate($limit);
            }

            //echo $this->model->getLastSql();
            /*foreach ($list as $k => $v) {
                $v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname);
                $v->hidden(['password', 'salt']);
            }*/
            $sum_price = 0;
            foreach ($list as $k => $v) {
                $sum_price += Db::name("recovery")->where(['uid'=>$v['uid']])->sum("price");
                $refer_data = Db::name("refer")->where(['uid'=>$v['uid']])->order("id desc")->find();
                if($v['refer_look_time'] < $refer_data['createtime']){
                    $list[$k]['is_look'] = 'no';
                }else{
                    $list[$k]['is_look'] = '';
                }


            }
            $result = array("total" => $list->total(),"sum_price" => $sum_price, "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }
    public function report($ids=null)
    {
        $material_data = $this->model->where(['id'=>$ids])->find();
        $report_data = Db::name("report")->where(['uid'=>$material_data['uid']])->select();
        $re_data['list'] = $report_data;
        $this->view->assign("row", $re_data);
        return $this->view->fetch();
    }
    public function refer($ids=null)
    {
        $material_data = $this->model->where(['id'=>$ids])->find();
        $data = Db::name("refer")->where(['uid'=>$material_data['uid']])->select();
        $user_data = Db::name("user")->where(['id'=>$material_data['uid']])->find();
        Db::name("material")->where(['id'=>$material_data['id']])->update(['refer_look_time'=>time()]);
        $log = '';
        foreach ($data as $k=>$v){
            $content = $v['content'];
            if($v['type'] == 'two'){
                $content = cdnurl($v['content'], true);

            }
            $data[$k]['content'] = $content;
            if($v['status'] == 'one'){
                $data[$k]['name'] = "用户";
                $log .= "用户-".$user_data['nickname'].":".$content."<br>";
            }else{

                $admin_data = Db::name("admin")->where(['id'=>$v['admin_id']])->find();
                $log .= "管理员-".$admin_data['nickname'].":".$content."<br>";
                $data[$k]['name'] = "管理员".$admin_data['nickname'];
            }
        }
        $this->view->assign("row", $data);
        $this->view->assign("log", $log);
        $this->view->assign("ids", $ids);
        return $this->view->fetch();
    }
    public function referRe(){
        $id = $this->request->post('id');
        $content = $this->request->post('content');
        if(!$id || !$content){
            $this->error("缺少比穿参数");
        }
        $data = Db::name("material")->where(['id'=>$id])->find();
        $in_data['uid'] = $data['uid'];
        $in_data['admin_id'] = $this->auth->id;
        $in_data['type'] = "one";
        $in_data['status'] = "two";
        $in_data['content'] = $content;
        $in_data['createtime'] = time();
        $in_status = Db::name("refer")->insertGetId($in_data);
        if($in_status){
            $this->success("回复成功");
        }else{
            $this->error("回复失败");
        }
    }
    public function referimage($ids=null){
        $id = $this->request->post('id');
        $content = $this->request->post('content');
        if(!$content){
            $this->view->assign("ids", $ids);
            return $this->view->fetch();
        }
        list($width, $height) = getimagesize(ROOT_PATH.'public'.'/uploads/20240902/91f95438ebd41470dc9f509f3a113922.jpeg');
       // echo "宽度: " . $width . " 高度: " . $height;exit;
        if(!$id || !$content){
            $this->error("缺少比穿参数");
        }
        $data = Db::name("material")->where(['id'=>$id])->find();
        $in_data['uid'] = $data['uid'];
        $in_data['admin_id'] = $this->auth->id;
        $in_data['type'] = "three";
        $in_data['status'] = "two";
        $in_data['content'] = $content;
        $in_data['width'] = $width;
        $in_data['height'] = $height;
        $in_data['createtime'] = time();
        $in_status = Db::name("refer")->insertGetId($in_data);
        if($in_status){
            $this->success("回复成功");
        }else{
            $this->error("回复失败");
        }
    }
}
